<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Contact extends Admin_Controller{
	
	public function __construct(){
		parent::__construct();
		$this->loadData('controllerName',$this->router->fetch_class());
	}
	
	public function index(){
		redirect('administrator/contact/unreply');
	}
	
	//Quan ly lien he chua phan hoi
	public function unreply(){
		$this->headTitle('Liên hệ chưa phản hồi - Admin Panel');
		$this->loadData('action',array('name' => 'unreply', 'display_name' => 'Liên hệ chưa phản hồi'));
		$this->manager(1, 'unreply');
	}
	
	//Quan ly lien he da phan hoi
	public function replied(){
		$this->headTitle('Liên hệ đã phản hồi - Admin Panel');
		$this->loadData('action',array('name' => 'replied', 'display_name' => 'Liên hệ đã phản hồi'));
		$this->manager(2, 'replied');
	}
	
	//Quan ly lien he
	public function manager($status, $actionname){
		$per_page = 20;
		$num_link = 7;
		$this->load->helper('form');
		$this->load->library('form_validation');
		$this->loadScript(base_url('js/admin/jquery.modify.js'));
	
		$this->load->model('Contacts_Model');
		/*$fromdate = $this->input->get('fromdate', true);
		if(empty($fromdate)){
			$fromdate = date('d-m-Y');
		}
		$todate = $this->input->get('todate', true);
		if(empty($todate)){
			$todate = date('d-m-Y');
		}

		$this->loadData('fromdate', $fromdate);
		$this->loadData('todate', $todate);
		$this->loadData('listcontacts', $this->Contacts_Model->getContactsByDate($fromdate, $todate));
		$date_start = $this->Contacts_Model->getMinDateOfContact();
		if(!$date_start){
			$date_start = date('d-m-Y');
		}
		$this->loadData('date_start', $date_start);
		*/
		$currentPage = $this->input->get('page',true);
		$currentPage = (!empty($currentPage))?$currentPage:'1';
		$countcontact = $this->Contacts_Model->countContact($status);
		$this->loadData('countcontact', $countcontact);
		//$this->loadData('listcontacts', $this->Contacts_Model->getContacts($status));	
		$pagingConfig = $this->init_pagination('administrator/contact/'.$actionname.'?act=paging', $countcontact, $per_page, $num_link);
		$this->loadData('paging', $this->pagination->create_links());
		$this->loadData('listcontacts', $this->Contacts_Model->getContactsLimit($status,(($currentPage-1) * $pagingConfig['per_page']), $pagingConfig['per_page']));	
		if($this->input->post()){
			if($this->form_validation->run('formsearch') == true){
				$keysearch = trim($this->input->post('keysearch', true));
				$fieldsearch = trim($this->input->post('field_search', true));
				$this->loadData('textkeysearch', $keysearch);
				$this->loadData('paging', '');
				$this->loadData('listcontacts', $this->Contacts_Model->searchContacts($keysearch, $fieldsearch, $status));
			}

		}
		$this->loadView('administrator/contact/manager');
	
	}
	//Chi tiet lien he
	public function detail(){
		$this->load->helper('form');
		$this->load->library('form_validation');
		$this->load->model('Contacts_Model');
		$conid = $this->input->get('conid', true);
		$contact = $this->Contacts_Model->getContactByConId($conid);
		if($contact){
			$this->loadData('contact', $contact);
			if($this->input->post()){
				if($this->form_validation->run('contact') == true){							
					if($this->Contacts_Model->updateContact($conid)){
						redirect('administrator/contact/unreply');		
					}
				}		
			}
			$this->loadScript(base_url('plugins/ckeditor/ckeditor.js'));
			$this->loadScript(base_url('js/admin/jquery.fixedListcontrol.js'));
			$this->headTitle('Chi tiết liên hệ - Admin Panel');
			$this->loadView('administrator/contact/detail');		
		}else{
			redirect('administrator/contact/unreply');
		}
	}
	
	//Xoa lien he
	public function delete(){
		if($this->input->post()){
			$key = $this->input->post('key', true);
			$this->load->model('Contacts_Model');		
			if($this->Contacts_Model->deleteContact($key)){
				$json["success"] = true;
			}else{
				$json["success"] = false;
			}
			echo json_encode($json);
		}
	}
	
}